package cn.iocoder.yudao.module.exam.controller.admin.knowledgegraph;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.knowledgegraph.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.knowledgegraph.KnowledgeGraphDO;
import cn.iocoder.yudao.module.exam.service.knowledgegraph.KnowledgeGraphService;

@Tag(name = "管理后台 - 知识图谱")
@RestController
@RequestMapping("/exam/knowledge-graph")
@Validated
public class KnowledgeGraphController {

    @Resource
    private KnowledgeGraphService knowledgeGraphService;

    @PostMapping("/create")
    @Operation(summary = "创建知识图谱")
    @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:create')")
    public CommonResult<Integer> createKnowledgeGraph(@Valid @RequestBody KnowledgeGraphSaveReqVO createReqVO) {
        return success(knowledgeGraphService.createKnowledgeGraph(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新知识图谱")
    @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:update')")
    public CommonResult<Boolean> updateKnowledgeGraph(@Valid @RequestBody KnowledgeGraphSaveReqVO updateReqVO) {
        knowledgeGraphService.updateKnowledgeGraph(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识图谱")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:delete')")
    public CommonResult<Boolean> deleteKnowledgeGraph(@RequestParam("id") Integer id) {
        knowledgeGraphService.deleteKnowledgeGraph(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除知识图谱")
                @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:delete')")
    public CommonResult<Boolean> deleteKnowledgeGraphList(@RequestParam("ids") List<Integer> ids) {
        knowledgeGraphService.deleteKnowledgeGraphListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识图谱")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:query')")
    public CommonResult<KnowledgeGraphRespVO> getKnowledgeGraph(@RequestParam("id") Integer id) {
        KnowledgeGraphDO knowledgeGraph = knowledgeGraphService.getKnowledgeGraph(id);
        return success(BeanUtils.toBean(knowledgeGraph, KnowledgeGraphRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得知识图谱分页")
    @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:query')")
    public CommonResult<PageResult<KnowledgeGraphRespVO>> getKnowledgeGraphPage(@Valid KnowledgeGraphPageReqVO pageReqVO) {
        PageResult<KnowledgeGraphDO> pageResult = knowledgeGraphService.getKnowledgeGraphPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgeGraphRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识图谱 Excel")
    @PreAuthorize("@ss.hasPermission('exam:knowledge-graph:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKnowledgeGraphExcel(@Valid KnowledgeGraphPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KnowledgeGraphDO> list = knowledgeGraphService.getKnowledgeGraphPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "知识图谱.xls", "数据", KnowledgeGraphRespVO.class,
                        BeanUtils.toBean(list, KnowledgeGraphRespVO.class));
    }

}